package com.jixiang.tracedata.util;

import com.travelsky.TUMSMQ.Zlib;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author quanbiao
 * @date 2018-09-10
 * 解压航信数据
 */
public final class HxCompressCode {

    private static final Logger log = LoggerFactory.getLogger("HxCompressCode");

    public static byte[] unCompressMsg(byte[] bMsg) {
        String comptypebMsg;
        String typebMsg;

        try {
            typebMsg = new String(bMsg, "iso-8859-1");
            log.debug("放入的报文：" + typebMsg);
        } catch (Exception var33) {
            return null;
        }

        byte[] beginText = new byte[]{13, 10, 2};
        String beginTextStr = new String(beginText);
        byte[] endAddr = new byte[]{13, 10, 46};
        String endAddrStr = new String(endAddr);
        byte[] endText = new byte[]{3};
        new String(endText);
        byte[] enter = new byte[]{13, 10};
        String enterStr = new String(enter);
        int contentIndex = typebMsg.indexOf(beginTextStr);
        if (contentIndex == -1) {
            log.error("报文格式错误without od oa 02");
            return null;
        } else {
            int sourceAddrIndex = typebMsg.indexOf(endAddrStr);
            if (sourceAddrIndex == -1) {
                log.error("报文格式错误without od oa 2e");
                return null;
            } else if (contentIndex < sourceAddrIndex) {
                log.error("报文格式错误od oa 02 before od oa 2e");
                return null;
            } else {
                int msgend = typebMsg.length() - 1;
                String temp = typebMsg.substring(sourceAddrIndex, contentIndex);
                int endsource = temp.indexOf("/C");
                if (endsource == -1) {
                    log.error("此报文不是已压缩的报文");
                    return null;
                } else {
                    endsource += sourceAddrIndex;
                    String msg1 = typebMsg.substring(0, endsource);
                    String temp2 = typebMsg.substring(contentIndex + 3);
                    int typeindex = 8;
                    if (temp2.indexOf(enterStr) != 3) {
                        log.error("报文格式错误with msgtype error");
                        return null;
                    } else {
                        String msg3 = "";
                        String temp1 = typebMsg.substring(endsource, contentIndex);
                        int endcompinfo = temp1.indexOf(" ");
                        if (endcompinfo == -1) {
                            msg3 = typebMsg.substring(contentIndex, contentIndex + typeindex);
                        } else {
                            msg3 = typebMsg.substring(endsource + endcompinfo, contentIndex + typeindex);
                        }

                        boolean hasEnd = false;

                        int i;
                        for(i = bMsg.length - 3; i > contentIndex; --i) {
                            if (bMsg[i] == 13 && bMsg[i + 1] == 10 && bMsg[i + 2] == 3) {
                                hasEnd = true;
                                break;
                            }
                        }

                        if (!hasEnd) {
                            for(i = bMsg.length - 1; i > contentIndex; --i) {
                                if (bMsg[i] == 3) {
                                    hasEnd = true;
                                    break;
                                }
                            }
                        }

                        if (!hasEnd) {
                            log.error("报文格式错误");
                            return null;
                        } else {
                            byte[] indata = new byte[i - typeindex - contentIndex];
                            if (contentIndex < bMsg.length) {
                                for(int j = contentIndex + typeindex; j < i; ++j) {
                                    indata[j - contentIndex - typeindex] = bMsg[j];
                                }
                            }

                            Zlib m_zlib = Zlib.getInstance();
                            byte[] outdata = (byte[])null;

                            try {
                                outdata = m_zlib.uncompress(indata);
                            } catch (Exception var32) {
                                var32.printStackTrace();
                                return null;
                            }

                            comptypebMsg = msg1 + msg3;
                            byte[] ucBMsg = new byte[comptypebMsg.length() + outdata.length + 3];
                            byte[] retbMsg = new byte[comptypebMsg.length()];

                            try {
                                retbMsg = comptypebMsg.getBytes();
                                int head = retbMsg.length;

                                int jj;
                                for(jj = 0; jj < retbMsg.length; ++jj) {
                                    ucBMsg[jj] = retbMsg[jj];
                                }

                                for(jj = 0; jj < outdata.length; ++jj) {
                                    ucBMsg[head + jj] = outdata[jj];
                                }

                                ucBMsg[comptypebMsg.length() + outdata.length] = enter[0];
                                ucBMsg[comptypebMsg.length() + outdata.length + 1] = enter[1];
                                ucBMsg[comptypebMsg.length() + outdata.length + 2] = endText[0];
                                comptypebMsg = new String(ucBMsg);
                            } catch (Exception var34) {
                                var34.printStackTrace();
                                log.error("编码转换失败");
                                return null;
                            }

                            log.debug("解压缩后的报文：" + comptypebMsg);
                            return ucBMsg;
                        }
                    }
                }
            }
        }
    }
}
